package cn.Liujialin.user.controller;

import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.user.domain.LoginInfo;
import cn.Liujialin.user.query.LoginInfoQuery;
import cn.Liujialin.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * 登录管理接口
 *   接口只能返回数据，不能返回页面
 */

@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {
   
    @Autowired
    private ILoginInfoService loginInfoService;

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody LoginInfo loginInfo){
        try {
            if(loginInfo.getId() == null){
                loginInfoService.save(loginInfo);
            }else{
                loginInfoService.update(loginInfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("操作失败！");
        }
    }


    //删除   delete
    @DeleteMapping("/{id}")//使用{} 占位  //http://localhost:8080/dept/1
    public AjaxResult deleteById(@PathVariable("id") Long id){
        // 高级技师玩法
        try {
            loginInfoService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败！");
        }
    }

    /**
     * 如果前端传递的是数组，后端可以使用List 或者 数组接收
     * @return
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            loginInfoService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败！");
        }
    }

    //获取一条数据
    @GetMapping("/{id}")
    public LoginInfo queryById(@PathVariable("id") Long id){
        return loginInfoService.findById(id);
    }

    //查询所有
    @GetMapping
    public List<LoginInfo> queryAll(){
        return loginInfoService.findByAll();
    }

    //分页  + 高级查询  post
    @PostMapping
    public PageList<LoginInfo> queryPage(@RequestBody LoginInfoQuery query){
        return loginInfoService.queryPage(query);
    }



}
